package com.zhongen.oa.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.common.security.annotation.Inner;
import com.zhongen.oa.entity.NotifyAnnouncementEntity;
import com.zhongen.oa.entity.dto.NotifyAnnouncementDto;
import com.zhongen.oa.service.NotifyAnnouncementService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公告信息
 *
 * @author Code Generator
 * @date 2024-02-27 09:48:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/notifyAnnouncement")
@Tag(description = "notifyAnnouncement", name = "公告信息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class NotifyAnnouncementController {

	private final NotifyAnnouncementService notifyAnnouncementService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param notifyAnnouncement 公告信息
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_notifyAnnouncement_view')")
	public R getNotifyAnnouncementPage(@ParameterObject Page page,
			@ParameterObject NotifyAnnouncementDto notifyAnnouncement) {
		return R.ok(notifyAnnouncementService.queryNotifyAnnouncementList(page, notifyAnnouncement));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param notifyAnnouncement 公告信息
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/pageApp")
	public R getNotifyAnnouncementPageApp(@ParameterObject Page page,
			@ParameterObject NotifyAnnouncementDto notifyAnnouncement) {
		return R.ok(notifyAnnouncementService.queryNotifyAnnouncementListApp(page, notifyAnnouncement));
	}

	/**
	 * 根据用户分页查询
	 * @return
	 */
	@Operation(summary = "根据用户分页查询", description = "根据用户分页查询")
	@GetMapping("/userPage")
	public R getNotifyAnnouncementByUser(@ParameterObject Page page,
			@ParameterObject NotifyAnnouncementDto notifyAnnouncement) {
		return R.ok(notifyAnnouncementService.queryNotifyAnnouncementByUser(page, notifyAnnouncement));
	}

	/**
	 * 通过id查询公告信息
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_notifyAnnouncement_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(notifyAnnouncementService.getById(id));
	}

	/**
	 * 通过id查询公告信息
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/app/{id}")
	public R getByIdApp(@PathVariable("id") Long id) {
		return R.ok(notifyAnnouncementService.getById(id));
	}

	@GetMapping("/html")
	@Operation(summary = "资讯html页面")
	@Inner
	public void getHtml(@RequestParam("id") Long id, HttpServletResponse response) {
		notifyAnnouncementService.getNotifyHtml(id, response);
	}

	/**
	 * 新增公告信息
	 * @param notifyAnnouncement 公告信息
	 * @return R
	 */
	@Operation(summary = "新增公告信息", description = "新增公告信息")
	@SysLog("新增公告信息")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_notifyAnnouncement_add')")
	public R save(@RequestBody NotifyAnnouncementDto notifyAnnouncement) {
		return R.ok(notifyAnnouncementService.insertNotifyAnnouncement(notifyAnnouncement));
	}

	/**
	 * 新增公告信息
	 * @return R
	 */
	@GetMapping("/test")
	public R test() {
		return R.ok(notifyAnnouncementService.test());
	}

	/**
	 * 修改公告信息
	 * @param notifyAnnouncement 公告信息
	 * @return R
	 */
	@Operation(summary = "修改公告信息", description = "修改公告信息")
	@SysLog("修改公告信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_notifyAnnouncement_edit')")
	public R updateById(@RequestBody NotifyAnnouncementDto notifyAnnouncement) {
		return R.ok(notifyAnnouncementService.updateById(notifyAnnouncement));
	}

	/**
	 * 通过id删除公告信息
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除公告信息", description = "通过id删除公告信息")
	@SysLog("通过id删除公告信息")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_notifyAnnouncement_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(notifyAnnouncementService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param notifyAnnouncement 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_notifyAnnouncement_export')")
	public List<NotifyAnnouncementEntity> export(NotifyAnnouncementEntity notifyAnnouncement, Long[] ids) {
		return notifyAnnouncementService.list(Wrappers.lambdaQuery(notifyAnnouncement)
			.in(ArrayUtil.isNotEmpty(ids), NotifyAnnouncementEntity::getId, ids));
	}

}
